//
//  WQ_MainViewController.swift
//  xinlang
//
//  Created by 联创—王增辉 on 2019/3/18.
//  Copyright © 2019年 lcWorld. All rights reserved.
//

import UIKit

class WQ_MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setVC()
        
        registerNotification()

    }
    //MARK: - 注册通知
    func registerNotification() {
        NotificationCenter.default.addObserver(self, selector: #selector(logoutAction), name: Notification.Name(rawValue: logoutChangeNotfication), object: nil)
    }
    //退出登录
    @objc func logoutAction(notification:Notification) {
        WQ_accessTokenModel.accessTokenModel.remove()
        NotificationCenter.default.post(name: NSNotification.Name.init(loginChangeNotfication), object: nil)
    }
    
    //MAKR: - 添加视图
    func setVC() {
        let VCData = [["title":"首页","class":"WQ_HomeController","image":"tabbar_home","visitor":["icon":"","title":"edddd"]],
                      ["title":"新闻","class":"WQ_NewViewController","image":"tabbar_discover","visitor":["icon":"visitordiscover_image_message","title":""]],
                      ["title":"消息","class":"WQ_messageViewController","image":"tabbar_message_center","visitor":["icon":"visitordiscover_image_message","title":""]],
                      ["title":"我的","class":"WQ_MeViewController","image":"tabbar_profile","visitor":["icon":"visitordiscover_image_profile","title":""]]];
        for item in VCData {
            let vc = getTabbarItem(itemData: item)
            self .addChild(vc)
        }
    }
    //MARK: - 创建控制器
    func getTabbarItem(itemData:[String:Any]) -> UIViewController {
        
        
        
        guard let classStr = itemData["class"] as? String,let title = itemData["title"] as? String,let imageStr = itemData["image"] as? String,let vc = NSClassFromString(Bundle.main.nameSpace+"."+classStr),let visitorInfo = itemData["visitor"] else {
            return WQ_BaseViewController()
        }
        let image = UIImage.init(named: imageStr)?.withRenderingMode(.alwaysOriginal)
        let imageSelect = UIImage.init(named: imageStr + "_highlighted")?.withRenderingMode(.alwaysOriginal)

        let classVC = vc.class() as! WQ_BaseViewController.Type
        let rootVC = classVC.init()

        rootVC.title = title
        rootVC.tabBarItem.image = image
        rootVC.tabBarItem.selectedImage = imageSelect
    rootVC.tabBarItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.orange], for: .selected)
        rootVC.visitorInfo = (visitorInfo as! [String : String])
        let navi = WQ_NavigationViewController(rootViewController:rootVC)

        return navi
        
    }
    

}
